<template>
	<view  class="container">
		<uni-row class="textzhu">
			<uni-col :offset="7">
				<text>
					共
					<text class="zhu">{{count}}</text>
					注，
				</text>
				<text>
					共
					<text class="zhu">{{count * 2 }}</text>
					元
				</text>
			</uni-col>
		</uni-row>
			<view >
				<textarea id="resultTextarea" v-model="spliceAll" class="ctextarea"></textarea>
			</view>
		</uni-card>
		<uni-row   :gutter="26">
			<uni-col :span="8">
				<button class="button01" @click="toggleFunctionali">{{ this.textContent }}</button>
			</uni-col>
			<uni-col :span="8">
				<button class="button01" @click="dadi">导入大底</button>
			</uni-col>
			<uni-col :span="8">
				<button class="button01" @click="laji">直选垃圾</button>
			</uni-col>
		</uni-row>
		<uni-row class="anNiuBuJu" :gutter="26">
			<uni-col :span="8">
				<button class="button01" @click="copy">复制</button>
			</uni-col>
			<uni-col :span="8">
				<button   class="button01"   @click="hmtj">号码统计</button>
			</uni-col>
			<uni-col :span="8">
				<button class="button01" @click="dmz">胆码组</button>
			</uni-col>
			
		</uni-row>
		<uni-row class="anNiuBuJu"  :gutter="26">
			<uni-col :span="8">
				<button class="button01" @click="lmzh">两码组合</button>
			</uni-col>
			<uni-col :span="8">
				<button class="button01" @click="lmc">任意两码差</button>
			</uni-col>
			<uni-col :span="8">
				<button class="button01" @click="lmh">任意两码和</button>
			</uni-col>
			<uni-col :span="8">
				<button class="button01" @click="deleteRecoredById">清除本次记录</button>
			</uni-col>
			<uni-col :span="8">
				<button class="button01" @click="clearAll">清空全部</button>
			</uni-col>
		</uni-row>
	</view>
</template>

<script>
export default {
	data() {
		return {
			count:0,
			spliceAll: [], // 初始化spliceAll数组
			args: {},
			result: [],
			// 初始状态为功能A
			currentFunction: 'A',
			textContent: '直转组'
		};
	},
	onLoad(query) {
		// 解析参数中的spliceAll数组
		if (query.args) {
			this.args = JSON.parse(query.args);
			if(query.result){
				this.result = JSON.parse(query.result);
			}
			uni.setNavigationBarTitle({
				title: this.args.projectName
			});
			//this.getDataIntersection(this.args.type, this.args.optionType);
		}
	},
	onShow(){
		this.getDataIntersection(this.args.type, this.args.optionType);
	},
	methods: {
		clearAll(){
			let that = this;
			uni.showModal({
				title: '提示',
				content: '确定要删除所有记录信息吗？',
				success: function (res) {
					if (res.confirm) {
						// 用户点击确定
						let tt = that
						that.result = []
						that.spliceAll = []
						that.count =0
						that.$myRequest({
							method: 'delete',
							url: '/deleteRecordAll?delFlag='+that.args.delFlag+'&type='+that.args.type
							//data: data
						}).then((res) => {
							if (res.flag) {
								uni.showToast({
									title: '删除记录成功！'
								});
								tt.getDataIntersection(that.args.type, that.args.optionType);
							} else {
								uni.showToast({
									title: '删除记录失败！'
								});
							}
						});
			
						// 在这里执行删除操作
					} else if (res.cancel) {
						// 用户点击取消
						console.log('用户点击了取消');
					}
				}
			});
		},
		dmz(){
			uni.navigateTo({
				url: `/pages/lottery/laji/dmz?args=${JSON.stringify(this.args)}`
			});
		},
		lmzh(){
			uni.navigateTo({
				url: `/pages/lottery/laji/lmzh?args=${JSON.stringify(this.args)}`
			});
		},
		lmc(){
			uni.navigateTo({
				url: `/pages/lottery/laji/lmc?args=${JSON.stringify(this.args)}`
			});
		},
		lmh(){
			uni.navigateTo({
				url: `/pages/lottery/laji/lmh?args=${JSON.stringify(this.args)}`
			});
		},
		dadi() {
			this.args.spliceAll = this.spliceAll
			uni.navigateTo({
				url: `/pages/lottery/laji/tolead?args=${JSON.stringify(this.args)}`
			});
		},
		laji() {
			this.args.spliceAll = this.spliceAll
			// 导入大底逻辑
			uni.navigateTo({
				url: `/pages/lottery/laji/laji?args=${JSON.stringify(this.args)}`
			});
		},
		hmtj() {
			// 号码统计逻辑
			uni.navigateTo({
				url: `/pages/lottery/statistics/statistics?spliceAll=${JSON.stringify(this.spliceAll)}&args=${JSON.stringify(this.args)}`
			});
		},
		// 处理按钮点击事件，切换功能
		toggleFunctionali() {
			// console.log(currentFunction);
			// 根据当前功能切换到另外一个功能
			if (this.currentFunction === 'A') {
				// 切换到功能B
				this.currentFunction = 'B';
				this.textContent = '组转直';
				// 执行功能直转组的逻辑
				this.zhiZhuanZu();
			} else {
				// 切换到功能A
				this.currentFunction = 'A';
				this.textContent = '直转组';
				// 执行功能A的逻辑
				this.getDataIntersection(this.args.type, this.args.optionType);
			}
		},
		deleteRecoredById() {
			let that = this;
			uni.showModal({
				title: '提示',
				content: '确定要删除该记录信息吗？',
				success: function (res) {
					if (res.confirm) {
						// 用户点击确定
						that.$myRequest({
							method: 'delete',
							url: '/deleteRecordById?id='+ that.args.recordId+'&type='+that.args.type
							//data: data
						}).then((res) => {
							if (res.flag) {
								uni.showToast({
									title: '删除记录成功！'
								});
								that.getDataIntersection(that.args.type, that.args.optionType);
							} else {
								uni.showToast({
									title: '删除记录失败！'
								});
							}
						});

						// 在这里执行删除操作
					} else if (res.cancel) {
						// 用户点击取消
						console.log('用户点击了取消');
					}
				}
			});
		},
		getDataIntersection(type, optionType) {
			// 获取号码交集
			const deviceId = uni.getStorageSync("phone");
			let that = this
			this.$myRequest({
				method: 'get',
				url: '/getDataIntersection?type=' + type + '&optionType=' + optionType + '&deviceId=' + deviceId
			}).then((res) => {
				if (res.data) {
					if(that.result.length === 0){
						res.data.sort((a, b) => a - b);
						that.count = res.data.length
						let re = res.data.join(' ')
						that.spliceAll = re;
					}else{
						let arr2 = res.data.filter((value) => that.result.includes(value));
						that.spliceAll = arr2.join(' ');
						that.count = arr2.length
					}
				}else{
					if(that.result.length != 0){
						this.spliceAll = this.result.join(' ');
						this.count = this.result.length
					}
				}
				// else{
				// 	this.spliceAll = this.result.join(' ');
				// 	this.count = this.result.length
				// }
			});
		},
		zhiZhuanZu() {
			let splice = this.spliceAll.split(" ")
			let zz = []
			for (var i = 0; i < splice.length; i++) {
				// 将每个字符串按字符拆分成数组，转换为数字后排序，再拼接成字符串
				const array = splice[i].split('').map(Number);
				array.sort((a, b) => a - b);
				let result = array.join('');
				zz[i] = result;
			}
			// 对整个字符串数组进行排序
			zz.sort((a, b) => a.localeCompare(b));
			// 去重操作
			zz = zz.filter((value, index, self) => {
				return self.indexOf(value) === index;
			});
			this.count = zz.length
			 this.spliceAll = zz.join(' ')
		},
	
		copy() {
			// 复制逻辑
			const textToCopy = this.spliceAll;
			uni.setClipboardData({
				data: textToCopy,
				success: () => {
					uni.showToast({
						title: '复制成功',
						icon: 'success'
					});
				},
				fail: () => {
					uni.showToast({
						title: '复制失败',
						icon: 'none'
					});
				}
			});
		}
	}
};
</script>

<style>
.container {
  height: 100vh;
  padding: 10px;  
  background-color: #f6eee8;  
  border-radius: 10px;  
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); 
}
.textzhu {
	font-size: 21px;
	margin-bottom: 10px;
}
.zhu {
	color: red;
}
.spliceAllText {
	font-size: 20px;
	padding-top: 15px;
	margin-left: 5px;
}
 .ctextarea{
 	height:300px;
 	background-color: white;
 	border-radius: 10px;
 	width: 100%;            /* 设置宽度 */
 	padding: 10px;        /* 内边距 */
 	box-sizing: border-box; /* 包括内边距和边框 */
 }
.anNiuBuJu {
	margin-bottom: 10px;
}

.button01 {
	font-size: 10px;
	margin-top: 15px;
}
</style>
